package cn.tju.yanbo.calendar;

import java.util.Calendar;
import java.util.Date;


import cn.tju.yanbo.commons.BaseActivity;
import cn.tju.yanbo.commons.CalendarGestureListener;
import cn.tju.yanbo.commons.db.AppDatabase;

import cn.tju.yanbo.R;
import cn.tju.yanbo.datechoose.DateChooseActivity;
import cn.tju.yanbo.goodoccasion.GoodOccasionActivity;
import cn.tju.yanbo.homepage.HomepageActivity;
import cn.tju.yanbo.luckyday.LuckyDayActivity;
import cn.tju.yanbo.setting.SettingActivity;
import cn.tju.yanbo.utils.AppUtils;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class LunarCalendarActivity extends BaseActivity{
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lunar_calendar);
        
        this.initActivity(savedInstanceState);
        View view = findViewById(R.id.main_content_view);
        view.setOnTouchListener(new CalendarGestureListener(this));
        initCalendarView();
    }
    
    private void initCalendarView() {
        String sc = AppUtils.format("yyyy-MM-dd", new Date());
        String lc = AppDatabase.APP_DB().lunarCalendar(sc);
        
        Log.i("startup", lc);
        String [] ymd = AppUtils.ymd(lc);
        
        this.setCustomFont(R.id.lunar_calendar_day, AppUtils.font());
        this.setCustomFont(R.id.lunar_calendar_month, AppUtils.font());
        this.setCustomFont(R.id.lunar_calendar_year, AppUtils.font());

        this.setViewText(R.id.lunar_calendar_year, ymd[0]);
        this.setViewText(R.id.lunar_calendar_month, ymd[1]);
        this.setViewText(R.id.lunar_calendar_day, ymd[2]);
    }

    protected void registerIntent() {
        registerIntent(R.id.menu_homepage, HomepageActivity.class);
        registerIntent(R.id.menu_datechoose, DateChooseActivity.class);
        registerIntent(R.id.menu_good_occasion, GoodOccasionActivity.class);
        registerIntent(R.id.menu_lucky_day, LuckyDayActivity.class);
        registerIntent(R.id.menu_setting, SettingActivity.class);
    }

    @Override
    protected boolean hasDateView() {
        return true;
    }

    @Override
    protected Class<?> leftActivity() {
        return HomepageActivity.class;
    }

    @Override
    protected Class<?> rightActivity() {
        return DateChooseActivity.class;
    }

    @Override
    protected Drawable background() {
        return CACHED_PICTURES.get(R.drawable.lunar_calendar);
    }

    public void update(Calendar c) {
        String sc = AppUtils.format("yyyy-MM-dd", c.getTime());
        String lc = AppDatabase.APP_DB().lunarCalendar(sc);
        String [] ymd = AppUtils.ymd(lc);
        
        TextView yearView = (TextView)this.findViewById(R.id.lunar_calendar_year);
        TextView monthView = (TextView)this.findViewById(R.id.lunar_calendar_month);
        TextView dayView = (TextView)this.findViewById(R.id.lunar_calendar_day);
        yearView.setText(ymd[0]);
        monthView.setText(ymd[1]);
        dayView.setText(ymd[2]);
    }
}
